package com.lc.hongtai.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonInvestpay;
import com.lc.hongtai.conn.JsonInvestpaydo;
import com.lc.hongtai.conn.JsonProductpay;
import com.lc.hongtai.toast.LovelyToast;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;


public class PayActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivback;
    private TextView tv01;
    private TextView tv02;
    private ImageView iv01;
    private ImageView iv02;
    private EditText et01;


    private Button bt01;


    private String has_pin;
    private EditText et02;
    private List<JsonInvestpay.Info.BonusListContent> list = new ArrayList<>();
    private List<JsonInvestpay.Info.BonusListContent> list1 = new ArrayList<>();
    private boolean MODE = true;//默认投资，false为理财


    private TextView tv08;
    private double lM = 0.00;
    private double HM = 0.00;

    View view_last;

    private LinearLayout money_layout;
    private PopupWindow popWindow;
    private PopupWindow popWindow2;
    private LinearLayout daishou_layout;
    View lines1;
    View lines2;
    View lines3;

    private String getProduct;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        getProduct = getIntent().getStringExtra("product");
        this.tv08 = (TextView) findViewById(R.id.tv_08);

        view_last = findViewById(R.id.view_last);


        this.et02 = (EditText) findViewById(R.id.et_02);


        this.bt01 = (Button) findViewById(R.id.bt_01);

        this.et01 = (EditText) findViewById(R.id.et_01);
        this.iv02 = (ImageView) findViewById(R.id.iv_02);
        this.iv01 = (ImageView) findViewById(R.id.iv_01);
        this.tv02 = (TextView) findViewById(R.id.tv_02);
        this.tv01 = (TextView) findViewById(R.id.tv_01);
        this.ivback = (ImageView) findViewById(R.id.iv_back);


        bt01.setOnClickListener(this);

        money_layout = (LinearLayout) findViewById(R.id.money_layout);
        daishou_layout = (LinearLayout) findViewById(R.id.daishou_layout);
        lines1 = findViewById(R.id.lines1);
        lines2 = findViewById(R.id.lines2);


        // text_yuan = (TextView) findViewById(text_yuan);
        MODE = getIntent().getBooleanExtra("mode", true);
        if (MODE) {
            getAccessToken1();


        } else {

            getAccessToken2();


            tv08.setText("支付密码");
            et02.setHint("请输入支付密码");
            view_last.setVisibility(View.GONE);
            lines3.setVisibility(View.GONE);
        }
        tv01.setText(MyApplication.myPreferences.readAccount_money());
    }

    private void getAccessToken2() {
        new JsonAccessToken("index/product_pay", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initData2(info.access_token);
                    } else {
                        //UtilToast.show(PayActivity.this, "安全验证失败！");
                        LovelyToast.makeText(PayActivity.this, "安全验证失败!", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);

                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(PayActivity.this, "安全验证失败!", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this);

    }


    private void initData2(String access_token) {
        new JsonProductpay(access_token, getIntent().getStringExtra("id"), MyApplication.myPreferences.readUid(), new AsyCallBack<JsonProductpay.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonProductpay.Info info) throws Exception {
                if (info != null) {
                    tv01.setText(info.account_money);
                    tv02.setText(info.invest_range);
                    if (info.invest_range.contains("无限")) {
                        String[] m = info.invest_range.replace("元", "").split("-");
                        lM = Double.parseDouble(m[0]);
                        HM = 0.00;
                    } else {
                        String[] m = info.invest_range.replace("元", "").split("-");
                        lM = Double.parseDouble(m[0]);
                        HM = Double.parseDouble(m[1]);
                    }

                    tv02.setSelected(true);
                    bt01.setSelected(true);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                //  UtilToast.show(PayActivity.this, JsonProductpay.TOAST);
                LovelyToast.makeText(PayActivity.this, JsonProductpay.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                bt01.setSelected(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

            }

        }).execute(PayActivity.this, true);
    }

    private void getAccessToken1() {
//        new JsonAccessToken("index/invest_pay", new AsyCallBack<JsonAccessToken.Info>() {
        new JsonAccessToken("index/invest_pay", new AsyCallBack<JsonAccessToken.Info>() {

            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initData1(info.access_token);
                    } else {
                        // UtilToast.show(PayActivity.this, "安全验证失败！");
                        LovelyToast.makeText(PayActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //UtilToast.show(PayActivity.this, "安全验证失败！");
                LovelyToast.makeText(PayActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

            }
        }).execute(this);

    }

    private void initData1(String access_token) {
        new JsonInvestpay(access_token, getIntent().getStringExtra("id"), MyApplication.myPreferences.readUid(), new AsyCallBack<JsonInvestpay.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonInvestpay.Info info) throws Exception {
                if (info != null) {
                    tv01.setText(info.account_money);
                    // tv05.setText(info.money_experience + "元");
                    tv02.setText(info.invest_range);
                    String[] m = info.invest_range.replace("元", "").split("-");

                    try {
                        lM = Double.parseDouble(m[0]);
                        HM = Double.parseDouble(m[1]);
                    } catch (Exception e) {
                        e.getMessage();
                    }
                    tv02.setSelected(true);
                    has_pin = info.has_pin;

                    if (has_pin.equals("1")) {


                    }
                    bt01.setSelected(true);
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                // UtilToast.show(PayActivity.this, JsonInvestpay.TOAST);
                LovelyToast.makeText(PayActivity.this, JsonInvestpay.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

                bt01.setSelected(false);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

            }

        }).execute(PayActivity.this, true);

    }


    private void showPopupWindow2(View parent) {

        if (popWindow2 == null) {
            View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_05, null)));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop2(View view) {

        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册
        TextView text_moneycount = (TextView) view.findViewById(R.id.tv_msg);


//        text_moneycount.setText(text_moneycount.getText().toString());
        text_moneycount.setText(JsonInvestpaydo.TOAST);
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
           /*     MyApplication.myPreferences.clear();
                onBackPressed();*/

                //投资
                onBackPressed();
                MyApplication.INSTANCE.finishActivity(ManageFinancesActivity.class);
                MyApplication.INSTANCE.finishActivity(InvestmentActivity.class);
                finish();
                popWindow2.dismiss();

            }
        });
    }


    private void showPopupWindow(View parent) {

        if (popWindow == null) {
            View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_04, null)));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop(View view) {

        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册
        TextView text_moneycount = (TextView) view.findViewById(R.id.text_moneycount);
        text_moneycount.setText(getProduct);
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

             /*   MyApplication.INSTANCE.finishActivity(ManageFinancesActivity.class);
              finish();*/

                popWindow.dismiss();
                MyApplication.INSTANCE.finishActivity(InvestmentActivity.class);
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_01:
                if (TextUtils.isEmpty(et01.getText().toString().trim())) {
                    // ToastUtils.show(this, "请输入金额！");
                    LovelyToast.makeText(PayActivity.this, "请输入金额！", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);

                }/* else if ((et01.getText().toString().contains(".") && Double.parseDouble(et01.getText().toString()) < lM) || (!et01.getText().toString().contains(".") && Integer.parseInt(et01.getText().toString()) < lM) || (et01.getText().toString().contains(".") && Double.parseDouble(et01.getText().toString()) > HM && HM > 0.00) || (!et01.getText().toString().contains(".") && Integer.parseInt(et01.getText().toString()) > HM && HM > 0.00)) {
                 ToastUtils.show(this, "请输入支付额度范围内的金额！");
             }*/ else {
                    String pay_str = et01.getText().toString().trim();
//                    Double d = Double.valueOf(pay_str);
                    if (pay_str.contains(".")) {
                        int position = pay_str.length() - pay_str.indexOf(".") - 1;
                        if (position > 2) {
                            //  Toast.makeText(context, "您输入的充值数额不对，只能输入分的单位", Toast.LENGTH_SHORT).show();
                            LovelyToast.makeText(PayActivity.this, "您输入的充值数额不对，只能输入分的单位", 0, LovelyToast
                                    .SUCCESS, LovelyToast.SCALE);
                        } else {
                            investPay();
                        }
                    } else {
                        investPay();
                    }


                }



              /*  if (bt01.isSelected()) {
                    if (TextUtils.isEmpty(et01.getText().toString())) {
                        ToastUtils.show(this, "请输入金额！");
                    } else if ((et01.getText().toString().contains(".") && Double.parseDouble(et01.getText().toString()) < lM) || (!et01.getText().toString().contains(".") && Integer.parseInt(et01.getText().toString()) < lM) || (et01.getText().toString().contains(".") && Double.parseDouble(et01.getText().toString()) > HM && HM > 0.00) || (!et01.getText().toString().contains(".") && Integer.parseInt(et01.getText().toString()) > HM && HM > 0.00)) {
                        ToastUtils.show(this, "请输入支付额度范围内的金额！");
                    } else {
                        if (MODE) {
                            if (has_pin.equals("1") && TextUtils.isEmpty(et02.getText().toString())) {
                                ToastUtils.show(this, "请输入投资密码！");
                            } else {
                                investPay();
                            }
                        } else {
                            if (TextUtils.isEmpty(et02.getText().toString())) {
                                ToastUtils.show(this, "请输入支付密码！");
                            } else {
                                getproductPayAccessToken();
                            }
                        }
                    }
                }*/

                break;
        }
    }

    private void investPay() {


//        getInvestPayDoAccessToken(is_experience, member_interest_rate_id, bonus_id);
        getInvestPayDoAccessToken();
    }


    private void getInvestPayDoAccessToken() {
        new JsonAccessToken("index/invest_pay_do", new AsyCallBack<JsonAccessToken.Info>() {


            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initInvestPayDo(info.access_token);
                      /*  Log.d("access_token" + info.access_token, "access_token");
                        Log.d("is_experience" + is_experience, "is_experience");
                        Log.d("member_interest_rate_id" + member_interest_rate_id, "member_interest_rate_id");
                        Log.d("bonus_id" + bonus_id, "bonus_id");
*/
                    } else {
                        //  UtilToast.show(PayActivity.this, "安全验证失败！");
                        LovelyToast.makeText(PayActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // UtilToast.show(PayActivity.this, "安全验证失败！");
                LovelyToast.makeText(PayActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }

    private void initInvestPayDo(String access_token) {
        new JsonInvestpaydo(access_token, getIntent().getStringExtra("id"), MyApplication.myPreferences.readUid(), et01.getText().toString().trim(), new AsyCallBack() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
               /* if (o.equals("")) {
                    UtilToast.show(PayActivity.this, JsonInvestpaydo.TOAST);
                }*/
                Log.d(toast, "adslfkjj");

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                bt01.setSelected(true);

                if (JsonInvestpaydo.status.equals("1")) {
                    showPopupWindow(bt01);
                } else {
                    showPopupWindow2(bt01);
                }

                MyApplication.INSTANCE.finishActivity(InvestmentActivity.class);
                //    UtilToast.show(PayActivity.this, JsonInvestpaydo.TOAST);
                LovelyToast.makeText(PayActivity.this, JsonInvestpaydo.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  Toast.makeText(context, "充值失败", Toast.LENGTH_SHORT).show();

                LovelyToast.makeText(PayActivity.this, "充值失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);
    }


}
